今天繼續來示範文件上傳至雲端的程式碼
MainActivity的程式碼
package com.example.demogoogle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.api.Scope;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import java.util.Collection;
import java.util.Collections;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_SIGNIN_CODE = 400;
GoogleSignInClient client;
GoogleDriveServiceFunction googleDriveServiceFunction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestStoragePremission();
requestSignIn();
}
public void requestSignIn() {
GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestScopes(new Scope(DriveScopes.DRIVE_FILE),
new Scope(DriveScopes.DRIVE_APPDATA),
new Scope(DriveScopes.DRIVE))
.build();
client = GoogleSignIn.getClient(this, signInOptions);
startActivityForResult(client.getSignInIntent(), REQUEST_SIGNIN_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_SIGNIN_CODE && resultCode == RESULT_OK) {
handleSignInIntent(data);
}
}
private void handleSignInIntent(Intent data) {
GoogleSignIn.getSignedInAccountFromIntent(data)
.addOnSuccessListener(googleSignInAccount -> {
GoogleAccountCredential credential = GoogleAccountCredential
.usingOAuth2(this, Collections.singleton(DriveScopes.DRIVE_FILE));
credential.setSelectedAccount(googleSignInAccount.getAccount());
drive(credential);
})
.addOnFailureListener(e -> e.printStackTrace());
}
public void drive(GoogleAccountCredential credential) {
Drive googleDriveService =
new Drive.Builder(
AndroidHttp.newCompatibleTransport(),
new GsonFactory(),
credential)
.setApplicationName("Appname")
.build();
googleDriveServiceFunction = new GoogleDriveServiceFunction(googleDriveService);
}
}
上面的程式碼做完會有登入的功能,成功的話會出現以下畫面:
第二張圖片到第五張圖片對應程式碼裡面requestSignIn副程式裡面的.requestScopes,而他們的功能我們可以參考網址的解釋去對應要功能:
http://javadox.com/com.google.apis/google-api-services-drive/v3-rev12-1.18.0-rc/com/google/api/services/drive/DriveScopes.html
每一個Google帳戶的第一次登入會出現第二張~第四張圖片的畫面,如果某天不小心將程式清除儲存空間以後在登入同一支Google帳戶時就只會出現第一張圖片。
因為我們會有上傳檔案的動作,而Android 6.0以上的版本中,為了安全設計,就全面要求app要和使用者要求權限,於是我在程式碼中新增了requestStoragePremission副程式來要求使用者給app檔案存取權限,代碼如下:
public void requestStoragePremission() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
}
成功後會出現以下畫面
除了以上方法能拿到權限以外,我們也可以由"應用程式資訊"來設定權限許可。
這樣一來Google帳戶登入就完成了,我們明天開始做上傳檔案的功能。